package cn.micai.base.lambda;

import javax.swing.plaf.nimbus.State;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/21 15:58
 */
public class StreamsMapExample {

    public static void main(String [] args) {

        List<String> alpha = Arrays.asList("a", "b", "c", "d");

        // Before Java8
        List<String> alphaUpper = new ArrayList<>();
        for (String s : alpha) {
            alphaUpper.add(s.toUpperCase());
        }

        System.out.println(alpha); // [a,b,c,d]
        System.out.println(alphaUpper); // [A,B,C,D]

        // Java 8
        List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(collect); // [A,B,C,D]

        // Extra, streams apply to any data type.
        List<Integer> num = Arrays.asList(1,2,3,4,5);
        List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList());
        System.out.println(collect1); // [2,4,6,8,10]


        // List of objects -> List of String

        List<Staff> staffs = Arrays.asList(
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );

        // Before Java8
        List<String> result = new ArrayList<>();
        for (Staff s : staffs) {
            result.add(s.getName());
        }
        System.out.println(result); // [mkyong,jack,lawrence]

        // Java8
        List<String> collect2 = staffs.stream()
                .map(x -> x.getName())
                .collect(Collectors.toList());
        System.out.println(collect2); // [mkyong,jack,lawrence]


        // List of objects -> List of other objects

        /*List<StaffPublic> staffPublics = convertToStaffPublic(staffs);
        System.out.println(staffPublics);*/

        // convert inside the map() method directly.
        List<StaffPublic> collect3 = staffs.stream()
            .map(staff -> {
                StaffPublic staffPublic = new StaffPublic();
                staffPublic.setName(staff.getName());
                staffPublic.setAge(staff.getAge());
                if ("mkyong".equals(staff.getName())) {
                    staffPublic.setExtra("this field is for mkyong only!");
                }
                return staffPublic;
            }).collect(Collectors.toList());
        System.out.println(collect3);

    }

    // Before Java 8
    private static List<StaffPublic> convertToStaffPublic(List<Staff> staffs) {
        List<StaffPublic> staffPublics = new ArrayList<>();

        for (Staff staff : staffs) {
            StaffPublic staffPublic = new StaffPublic();
            staffPublic.setName(staff.getName());
            staffPublic.setAge(staff.getAge());
            if ("mkyong".equals(staff.getName())) {
                staffPublic.setExtra("this field is for mkyong only!");
            }
            staffPublics.add(staffPublic);
        }

        return staffPublics;

    }
}
